package com.wcs.exam.dao.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcs.exam.common.base.page.SqlUtil;
import com.wcs.exam.common.cache.CacheRedis;
import com.wcs.exam.common.constant.RedisConstant;
import com.wcs.exam.dao.po.PaperQuestionCiteCountPO;
import com.wcs.exam.dao.PaperQuestionDao;
import com.wcs.exam.dao.impl.mapper.PaperQuestionMapper;
import com.wcs.exam.dao.impl.mapper.entity.PaperQuestion;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 试卷试题 持久化实现类
 *
 * @author wcs
 * @since 2025-03-11
 */
@RequiredArgsConstructor
@Repository
public class PaperQuestionDaoImpl extends ServiceImpl<PaperQuestionMapper, PaperQuestion> implements PaperQuestionDao {

    @NotNull
    private final CacheRedis cacheRedis;

    @Override
    public <R> List<R> selectObjs(Wrapper<PaperQuestion> wrapper, Class<R> classType) {
        List<Map<String, Object>> resultMaps = baseMapper.selectMaps(wrapper);
        if (CollectionUtil.isEmpty(resultMaps)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(resultMaps, classType);
    }

    @Override
    public Integer getMaxSort() {
        return lambdaQuery().orderByDesc(PaperQuestion::getSort).last(SqlUtil.limitOne()).oneOpt().map(PaperQuestion::getSort).orElse(0);
    }

    @Override
    public List<PaperQuestion> listByPaperId(Long paperId) {
        return lambdaQuery()
                .eq(PaperQuestion::getPaperId, paperId)
                .orderByAsc(PaperQuestion::getTitleId)
                .orderByAsc(PaperQuestion::getSort)
                .list();
    }

    @Override
    public List<PaperQuestion> listByPaperIdForCache(Long paperId) {
        List<PaperQuestion> questions = cacheRedis.list(RedisConstant.Exam.PAPER_QUESTION + paperId, PaperQuestion.class);
        if (CollUtil.isEmpty(questions)) {
            questions = listByPaperId(paperId);
            cacheRedis.set(RedisConstant.Exam.PAPER_QUESTION + paperId, questions, 10, TimeUnit.SECONDS);
        }
        return questions;
    }

    @Override
    public List<PaperQuestion> listByTitleIds(Collection<Long> titleIds) {
        return lambdaQuery()
                .in(PaperQuestion::getTitleId, titleIds)
                .orderByAsc(PaperQuestion::getTitleId)
                .orderByAsc(PaperQuestion::getSort).list();
    }

    @Override
    public Boolean deleteByPaperId(Long paperId) {
        return lambdaUpdate()
                .eq(PaperQuestion::getPaperId, paperId)
                .remove();
    }

    @Override
    public Boolean deleteByPaperIds(Collection<Long> paperIds) {
        return lambdaUpdate()
                .in(PaperQuestion::getPaperId, paperIds)
                .remove();
    }

    @Override
    public List<PaperQuestionCiteCountPO> questionCiteCunt(Collection<Long> questionIds) {
        LambdaQueryWrapper<PaperQuestion> wrapper = new QueryWrapper<PaperQuestion>()
                .select("question_id as questionId, count(1) as count")
                .lambda()
                .in(PaperQuestion::getQuestionId, questionIds)
                .groupBy(PaperQuestion::getQuestionId);
        return selectObjs(wrapper, PaperQuestionCiteCountPO.class);
    }
}
